package com.tc.juc.pack06;

import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @Auther: tianchao
 * @Date: 2021/10/5 22:02
 * @Description:
 */
@Slf4j
public class MyTest36 {
    static AtomicReference<BigDecimal> atomicReference = new AtomicReference<>(new BigDecimal("100"));

    public static void main(String[] args) {
        log.info("main start....");
        BigDecimal prev = atomicReference.get();
        other();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("尝试 100 -> 500: {}", atomicReference.compareAndSet(prev, new BigDecimal("500")));
        log.info("修改后的值: {}", atomicReference.get());
    }

    private static void other() {
        new Thread(()->{log.info("尝试 100 -> 200: {}",atomicReference.compareAndSet(atomicReference.get(), new BigDecimal("110")));}).start();
        new Thread(()->{log.info("尝试 200 -> 100: {}",atomicReference.compareAndSet(atomicReference.get(), new BigDecimal("100")));}).start();
    }
}
